package org.jacp.demo.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import org.jacp.demo.entities.Employee;

public class EmployeeTableModel extends AbstractTableModel {

    /**
     * 
     */
    private static final long serialVersionUID = -4971814034395614256L;
    public static final int EMPID = 0;
    public static final int FIRSTNAME = 1;
    public static final int MIDINT = 2;
    public static final int LASNAME = 3;
    public static final int PHONENO = 4;
    public static final int JOB = 5;
    public static final int EDEVEL = 6;
    public static final int SEX = 7;
    public static final int SALARY = 8;
    public static final int BONUS = 9;
    public static final int COMMISSSION = 10;
    public static final int HIDDEN_INDEX = 11;

    protected String[] columnNames;
    protected List<Employee> dataVector;

    public EmployeeTableModel(String[] columnNames) {
	this.columnNames = columnNames;
	dataVector = new ArrayList<Employee>();
    }
    
    public EmployeeTableModel(String[] columnNames, List<Employee> employees) {
	this.columnNames = columnNames;
	dataVector = employees;
    }

    public String getColumnName(int column) {
	return columnNames[column];
    }

    public boolean isCellEditable(int row, int column) {
	if (column == HIDDEN_INDEX)
	    return false;
	return true;
    }

    public Class<?> getColumnClass(int column) {
	switch (column) {
	case EMPID:
	    return String.class;
	case FIRSTNAME:
	    return String.class;
	case MIDINT:
	    return String.class;
	case LASNAME:
	    return String.class;
	case PHONENO:
	    return String.class;
	case JOB:
	    return String.class;
	case EDEVEL:
	    return Integer.class;
	case SEX:
	    return Character.class;
	case SALARY:
	    return Double.class;
	case BONUS:
	    return Double.class;
	case COMMISSSION:
	    return Double.class;
	default:
	    return Object.class;
	}
    }
    
    public Employee getRecordAt(int row) {
	return dataVector.get(row);
    }

    public Object getValueAt(int row, int column) {
	Employee record = dataVector.get(row);
	switch (column) {
	case EMPID:
	    return record.getEmpid();
	case FIRSTNAME:
	    return record.getFirstName();
	case MIDINT:
	    return record.getMidInitial();
	case LASNAME:
	    return record.getLastName();
	case PHONENO:
	    return record.getPhoneNumber();
	case JOB:
	    return record.getJob();
	case EDEVEL:
	    return record.getEducationLevel();
	case SEX:
	    return record.getSex();
	case SALARY:
	    return record.getSalary();
	case BONUS:
	    return record.getBonus();
	case COMMISSSION:
	    return record.getCommission();
	default:
	    return new Object();
	}
    }

    public void setValueAt(Object value, int row, int column) {
	Employee record = dataVector.get(row);
/*	switch (column) {
	case TITLE_INDEX:
	    record.setTitle((String) value);
	    break;
	case ARTIST_INDEX:
	    record.setArtist((String) value);
	    break;
	case ALBUM_INDEX:
	    record.setAlbum((String) value);
	    break;
	default:
	    System.out.println("invalid index");
	}*/
	System.out.println(row+" HALLO: "+column);
	//fireTableCellUpdated(row, column);
    }

    public int getRowCount() {
	return dataVector.size();
    }

    public int getColumnCount() {
	return columnNames.length;
    }

    public boolean hasEmptyRow() {
	if (dataVector.size() == 0)
	    return false;
	Employee audioRecord = dataVector.get(dataVector.size() - 1);
	if (audioRecord.getFirstName().trim().equals("")
		&& audioRecord.getLastName().trim().equals("")
		&& audioRecord.getMidInitial().trim().equals("")) {
	    return true;
	} 
	
	return false;
    }

    public void addEmptyRow() {
	dataVector.add(new Employee());
	fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
    }
    
    public void addEntry(Employee employee) {
	dataVector.add(employee);
	fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
    }
    
    public void flush() {
	dataVector.clear();
    }
    
    public void addEntries(List<Employee> employees) {
	dataVector.addAll(employees);
	fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
    }

}
